package com.web.controller.student;

import com.common.apiresult.CommonResult;
import com.common.apiresult.ResultCode;
import com.common.domain.dto.StudentInfoQueryDTO;
import com.common.domain.vo.StudentInfoPageVO;
import com.common.domain.vo.StudentInfoVO;
import com.service.serviceInterface.StudentInfoService;
import com.service.utils.SecurityUtil;
import com.system.mapper.DormMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author liuxuhuang
 * @date 2023年04月04日 10:44
 * @desc 学生信息控制器
 */
@RestController
@Api(value = "studentInfo", tags = "学生信息控制器")
@RequestMapping(value = "/student")
@Slf4j
public class StudentInfoController {
    @Resource
    private SecurityUtil securityUtil;
    @Resource
    private StudentInfoService studentInfoService;
    @Resource
    private DormMapper dormMapper;

    @GetMapping(value = "/getBuildStudentInfo")
    @ApiOperation(value = "获取同一楼宇的学生信息")
    public CommonResult getBuildStudentInfo(StudentInfoQueryDTO dto) {
        long userId = securityUtil.getUserId(SecurityContextHolder.getContext());
        List<StudentInfoVO> list = studentInfoService.selectSameBuild(dto, userId);
        if (list.size() <= 0) {
            return CommonResult.failed(ResultCode.RETURNNULL);
        }
        for (StudentInfoVO vo : list) {
            vo.setDormName(dormMapper.queryNameById(vo.getDormId()));
        }
        StudentInfoPageVO pageVO = new StudentInfoPageVO();
        pageVO.setVo(list).setTotal(list.size());
        return CommonResult.success(pageVO);
    }
}
